package ambiente_grafico;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import logica.mundo.Bloque;
import logica.mundo.Punto;
import logica.mundo.SimuladorTerremoto;

/**
 *
 * @author JorgeIvan
 */
public class Lienzo extends javax.swing.JPanel implements Runnable{

    private ImageIcon fondo;
    private SimuladorTerremoto simulador;
    private int cont,cont2,contImag;
    private double resis;

    public Lienzo() {
        initComponents();
        this.fondo=new ImageIcon(new ImageIcon(getClass().getResource("/data/fondo.jpg")).getImage());
        this.simulador=new SimuladorTerremoto(getPreferredSize());
        new Thread(this.simulador).start();
        new Thread(this).start();
        this.resis=simulador.getEdificio().getResistencia()/5;
        this.cont=0;
        this.cont2=4;
        this.contImag=1;
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        silGrado = new javax.swing.JSlider();
        silVelocidad = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(600, 400));

        silGrado.setValue(0);
        silGrado.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                silGradoStateChanged(evt);
            }
        });

        silVelocidad.setValue(0);
        silVelocidad.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                silVelocidadStateChanged(evt);
            }
        });

        jLabel2.setText("Velocidad");

        jLabel3.setText("Grado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(silGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                .addComponent(silVelocidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(114, 114, 114))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(191, 191, 191))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(615, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(silVelocidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(silGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void silGradoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_silGradoStateChanged
        // TODO add your handling code here:
        simulador.setGrado(silGrado.getValue());
    }//GEN-LAST:event_silGradoStateChanged

    private void silVelocidadStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_silVelocidadStateChanged
        // TODO add your handling code here:
        simulador.setVelocidad(Math.abs(100-silVelocidad.getValue()));
    }//GEN-LAST:event_silVelocidadStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSlider silGrado;
    private javax.swing.JSlider silVelocidad;
    // End of variables declaration//GEN-END:variables


    @Override
    public void paint(Graphics g) {
        
        simulador.getEdificio().setLim(getSize());
        
        if(simulador.getEdificio().getResistencia()<=resis*cont2)
        {
            simulador.getEdificio().setImagen(new ImageIcon(new ImageIcon(getClass().getResource("/data/edificio"+contImag+".png")).getImage()));
            
            if(cont<4)
            {
             cont++;
             new Thread(simulador.getEdificio().getBloques()[cont-1]).start();
             cont2--;
             contImag++;   
            }
            else
            {
                for (int i = 0; i < simulador.getEdificio().getBloques().length-4; i++) {
                    new Thread(simulador.getEdificio().getBloques()[i+4]).start();
                }
            }
        }
        for (int i = 0; i < cont; i++) {
           simulador.getEdificio().getBloques()[i].dibujar(g);
        }
        
        g.drawImage(this.fondo.getImage(), 0, 0, getSize().width,getSize().height,null);
        this.setOpaque(false);
        simulador.dibujar(g);
        super.paint(g);
  
    }
    @Override
    public void run() {
        
        while(true)
        {
            repaint();
        }
        
    }
}
